
/**
 * created on 2012-05-06
 * @author  qiujz
 * @version 1.0
 *
 * Copyright (c)  Founder Software (Suzhou) Co., Ltd.  2012
 *  All rights reserved.
 *
 * 
 * ================================================================
 * Change Revision
 * ---------------------------------------------------------------
 *    DATE                BY                   COMMENT
 * ---------------------------------------------------------------
 * 2012-05-06         qiujz                Create class
 * 
 *
 * ================================================================
 */

package com.qjz.service.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.founder.mh.framework.entity.QueryResultBase;
import com.founder.mh.framework.util.BuildTableUtil;
import com.qjz.dao.IHigherGradeDao;
import com.qjz.dao.IStudentInfoDao;
import com.qjz.entity.HigherGradeQueryParameter;
import com.qjz.entity.HigherGradeTo;
import com.qjz.entity.StudentInfoTo;
import com.qjz.service.IHigherGradeService;
import com.qjz.util.ExcelUtil;
import com.founder.mh.framework.entity.Sort;
import com.founder.mh.framework.service.impl.BaseServiceImpl;

@WebService(endpointInterface = "com.qjz.service.IHigherGradeService")
public class HigherGradeServiceImpl extends BaseServiceImpl implements IHigherGradeService {

	private static final Log LOGGER = LogFactory.getLog(HigherGradeServiceImpl.class);

	private IHigherGradeDao higherGradeDao;
	
	/**
	 * @return Dao接口
	 */
	public IHigherGradeDao getHigherGradeDao() {
		return higherGradeDao;
	}
	
	/**
	 * @param higherGradeDao
	 *            Dao接口
	 */
	public void setHigherGradeDao(IHigherGradeDao higherGradeDao) {
		this.higherGradeDao = higherGradeDao;
	}
	
	private IStudentInfoDao studentInfoDao;

	/**
	 * @return Dao接口
	 */
	public IStudentInfoDao getStudentInfoDao() {
		return studentInfoDao;
	}

	/**
	 * @param studentInfoDao
	 *            Dao接口
	 */
	public void setStudentInfoDao(IStudentInfoDao studentInfoDao) {
		this.studentInfoDao = studentInfoDao;
	}
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IHigherGradeService
	 *      #searchHigherGradeList(com.qjz.entity.HigherGradeQueryParameter)
	 */
	public QueryResultBase searchHigherGradeList(HigherGradeQueryParameter paramter) {
		if (paramter == null) {
			throw new RuntimeException("查询参数对象不能为空!");
		}
		
		// 放置查询结果的对象
		QueryResultBase rtnResult = new QueryResultBase();

		// 设置查询参数
		Map<String, Object> paramMap = new HashMap<String, Object>();
		HigherGradeTo higherGrade=paramter.getHigherGrade();
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		paramMap.put(BuildTableUtil
				.buildPropertyNameFromColumn(HigherGradeTo.NIAN_DU), year);
		if(null != higherGrade){
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(HigherGradeTo.XING_MING), 
					higherGrade.getXingMing());
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(HigherGradeTo.STUDENT_ID), 
					higherGrade.getStudentId());
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(HigherGradeTo.YUAN_XUE_XIAO), 
					higherGrade.getYuanXueXiao());
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(HigherGradeTo.BAN_JI), 
					higherGrade.getBanJi());

			if(higherGrade.getXinXiaoId() != 0)
				paramMap.put(BuildTableUtil
								.buildPropertyNameFromColumn(HigherGradeTo.XIN_XIAO_ID), 
								higherGrade.getXinXiaoId());
			///
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(HigherGradeTo.LEI_XING), 
					higherGrade.getLeiXing());
		}

		List<HigherGradeTo> higherGradeList = null;
		int total = higherGradeDao.getHigherGradeCount(paramMap);
		rtnResult.setTotal(total);
		if (total == 0) {
			return rtnResult;
		}
		
		// 设置排序字段
		List<Sort> sortList = paramter.getSort();
		if (sortList != null && sortList.size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (Sort sort : sortList) {
				sb.append(sort.getColumnName()).append(" ").append(
						sort.getOrder()).append(",");
			}

			paramMap.put("sort", sb.toString()
					.substring(0, sb.lastIndexOf(",")));

			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug(paramMap.get("sort"));
			}
		}
		
		// 不分页的情况
		if (paramter.getStartRow() <= 0) {
			higherGradeList= higherGradeDao.searchHigherGradeList(paramMap);
		} else {
			// 执行分页询
			int endRow = paramter.getStartRow() + paramter.getLimit() - 1;
			higherGradeList = higherGradeDao.searchHigherGradePagination(paramMap,
					paramter.getStartRow(), endRow);
		}
		rtnResult.setPageData(higherGradeList);
		return rtnResult;
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IHigherGradeService
	 *      #getHigherGradeByPrimaryKey(int)
	 */
	public HigherGradeTo getHigherGradeByPrimaryKey(int hgid) {
		return higherGradeDao.getHigherGradeByPrimaryKey(hgid);
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IHigherGradeService
	 *      #insertHigherGrade(com.qjz.entityHigherGrade.HigherGradeTo)
	 */
	public HigherGradeTo insertHigherGrade(HigherGradeTo higherGrade) {		
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("nianDu", year);
		paramMap.put("studentId", higherGrade.getStudentId());
		paramMap.put("leiXing", higherGrade.getLeiXing());
		higherGradeDao.deleteHigherGradeByMap(paramMap);
		
		higherGrade.setNianDu(year);
		higherGradeDao.insertHigherGrade(higherGrade);		
		
		return higherGrade;
	}
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IHigherGradeService
	 *      #updateHigherGrade(com.qjz.entity.HigherGradeTo)
	 */
	public int updateHigherGrade(HigherGradeTo higherGrade) {
		return higherGradeDao.updateHigherGradeByBean(higherGrade);		
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IHigherGradeService
	 *      #deleteHigherGrade(int)
	 */
	public int deleteHigherGrade(int hgid) {
		return higherGradeDao.deleteHigherGradeByPrimaryKey(hgid);
	}
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IHigherGradeService
	 *      #deleteHigherGradeBatch(int[])
	 */
	public void deleteHigherGradeBatch(int[] keyArray) {
		higherGradeDao.deleteHigherGradeBatch(keyArray);
	}
	
	public void submitSjqInfo(List<HigherGradeTo> list, HigherGradeQueryParameter paramter){
		HigherGradeTo higherGrade=paramter.getHigherGrade();
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("nianDu", year);
		paramMap.put("yuanXueXiao", higherGrade.getYuanXueXiao());
		paramMap.put("banJi", higherGrade.getBanJi());
		paramMap.put("leiXing", higherGrade.getLeiXing());
		higherGradeDao.deleteHigherGradeByMap(paramMap);
		
		for(HigherGradeTo hg : list){
			hg.setNianDu(year);
			hg.setYuanXueXiao(higherGrade.getYuanXueXiao());
			hg.setLeiXing(higherGrade.getLeiXing());
			hg.setZhuangTai(1);
			hg.setZhuangTaiMing("等待验证");
			
			higherGradeDao.insertHigherGrade(hg);
		}
	}
	
	public void modifySjqInfo(List<HigherGradeTo> list){
		for(HigherGradeTo hg : list){
			higherGradeDao.updateHigherGradeByBean(hg);
		}
	}
	
	public List<HigherGradeTo> searchSJQLRList(HigherGradeQueryParameter paramter){
		HigherGradeTo higherGrade=paramter.getHigherGrade();
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("nianDu", year);
		paramMap.put("yuanXueXiao", higherGrade.getYuanXueXiao());
		paramMap.put("banJi", higherGrade.getBanJi());
		paramMap.put("leiXing", higherGrade.getLeiXing());
		
		return higherGradeDao.searchSJQLRList(paramMap);
	}
	
	public String exportData(HigherGradeQueryParameter paramter){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		HigherGradeTo higherGrade=paramter.getHigherGrade();
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		paramMap.put(BuildTableUtil
				.buildPropertyNameFromColumn(HigherGradeTo.NIAN_DU), year);
		if(null != higherGrade){
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(HigherGradeTo.XING_MING), 
					higherGrade.getXingMing());
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(HigherGradeTo.LEI_XING), 
					higherGrade.getLeiXing());
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(HigherGradeTo.STUDENT_ID), 
					higherGrade.getStudentId());
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(HigherGradeTo.YUAN_XUE_XIAO), 
					higherGrade.getYuanXueXiao());
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(HigherGradeTo.BAN_JI), 
					higherGrade.getBanJi());

			if(higherGrade.getXinXiaoId() != 0)
				paramMap.put(BuildTableUtil
								.buildPropertyNameFromColumn(HigherGradeTo.XIN_XIAO_ID), 
								higherGrade.getXinXiaoId());
		}
		
		List<HigherGradeTo> higherGradeList = higherGradeDao.searchHigherGradeList(paramMap);
		return ExcelUtil.saveGeneratedExcel(ExcelUtil.hgDataExcel(higherGradeList), 12L, paramter.getExcelTitle());
	}
	
	public int hgDataIntoDataBase(int type){
		int result=0;
		Map<String, Object> paramMap = new HashMap<String, Object>();
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		paramMap.put(BuildTableUtil
				.buildPropertyNameFromColumn(HigherGradeTo.NIAN_DU), year);
		paramMap.put(BuildTableUtil
				.buildPropertyNameFromColumn(HigherGradeTo.LEI_XING), type);
		paramMap.put(BuildTableUtil
				.buildPropertyNameFromColumn(HigherGradeTo.ZHUANG_TAI), 2);
		List<HigherGradeTo> higherGradeList = higherGradeDao.searchHigherGradeList(paramMap);
		String shenFenZhengs = "";
		for(HigherGradeTo hg : higherGradeList){
			if (null != hg.getStudentId()
					&& !"".equals(hg.getStudentId()))
				shenFenZhengs += "'" + hg.getStudentId() + "',";
		}
		
		if (!"".equals(shenFenZhengs))
		{
			shenFenZhengs=shenFenZhengs.substring(0, shenFenZhengs.length() - 1);
			paramMap.clear();
			paramMap.put("shenFenZhengs", shenFenZhengs);
			List<StudentInfoTo> studentList=studentInfoDao.searchStudentInfoList(paramMap);
			paramMap.put("pType", 2);
			for (StudentInfoTo student : studentList) {
				for(HigherGradeTo hg : higherGradeList){
					if(hg.getXingMing().equals(student.getXingMing())
							&& hg.getStudentId().equals(student.getShenFenZheng())){
						student.setXueXiao(hg.getXinXiaoMing());
						student.setXueXiaoDaiMa(hg.getXinXiaoId()+"");
						student.setBanJi("");
						student.setRuXueNian(year + "-09");
						student.setXueXiaoLeiXing(Integer.parseInt(student.getXueXiaoLeiXing())+1 +"");
						
						paramMap.put("school", student.getXueXiaoDaiMa());
						paramMap.put("shenFenZhengs", "'"+student.getShenFenZheng()+"'");
						studentInfoDao.deleteStudentInfoByParamter(paramMap);
						
						studentInfoDao.insertStudentInfo(student);
						result++;
						continue;
					}
				}
			}
		}
		
		return result;
	}
}
	
